package com.vs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.dao.ProjectOperations;
import com.vs.model.CustomMessage;
import com.vs.model.ProjectForm;
import com.vs.util.Constants;

public class ShowProjectAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String next = "";
		ProjectForm projectForm = (ProjectForm) form;
		Constants.log.debug(projectForm);

		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();

		List<ProjectForm> list = new ProjectOperations().getAllProjectList();
		
		if (list.size() == 0) {
			CustomMessage info = new CustomMessage();
			info.setMessageLevel(Constants.INFO);
			info.setMessageText("No projects found in the system");
			customErrors.add(info);
		}
		
		HttpSession session = request.getSession(false);
		session.removeAttribute("projectName");
		session.removeAttribute("projectId");
		
		next = "success";
		request.setAttribute("customErrors", customErrors);
		request.setAttribute("projectList", list);
		System.out.println("added project list to request object");
		return mapping.findForward(next);

	}

}
